
<template>
  <div class="login" id="col">
    <van-nav-bar right-text="注册" @click-right="onClickRight" />
    <h1 class="logo">
      <img src="https://file.sdyxmall.com/h5/auth/public/app/img/logo.adec77d.png" alt />
    </h1>

    <van-field v-model="data.phone" center clearable placeholder="请输入注册的手机号码">
      <van-button
        slot="button"
        size="small"
        class="btn"
        @click="getSmsCode"
        color="linear-gradient(to right, #be3335, #f40)"
      >发送验证码</van-button>
    </van-field>

    <van-field v-model="data.smsCode" placeholder="请输入验证码" />

    <van-button class="dx-login" @click="submint" color="linear-gradient(to right, #be3335, #f40)">
      <van-loading v-if="show" size="40px">短信登陆</van-loading>
      <span v-else>短信登陆</span>
    </van-button>
  </div>
</template>
<script>
import { Toast } from "vant";
export default {
  name: "login",
  data() {
    return {
      data: {
        phone: "",
        smsCode: "",
        password: ""
      },

      show: false
    };
  },
  methods: {
    onClickRight() {
      this.$router.push("/register");
    },
    getSmsCode() {
      let smscode = /^1[3-9]\d{9}$/;
      let flag = smscode.test(this.data.phone);
      if (flag) {
        let data = this.data;
        let url = "/user/getSmsCode";
        this.$axios
          .post(url, data)
          .then(res => {
            this.toast3();
          })
          .catch(err => {
            this.$toast.fail("登陆失败");
          });
      } else {
        this.$toast("请输入正确的手机号码");
      }
    },
    submint() {
      let smscode = /^1[3-9]\d{9}$/;
      let flag = smscode.test(this.data.phone);
      if (flag) {
        let url = "/user/login";
        let data = this.data;
        this.$axios
          .post(url, data)
          .then(res => {
            this.show = true;
            let toast1 = Toast.success("登陆成功");
            this.$store.commit("updatephone", res.result.phone);
            this.$store.commit("updatetoken", res.result.token);
            this.$store.commit("updateuserId", res.result.userId);
            this.$store.commit("updateislogin", true);
            setTimeout(() => {
              this.show = false;
              toast1.clear();
              this.$router.push("/home");
            }, 1000);
          })
          .catch(err => {
            console.log("err", err);
          });
      } else {
        this.$toast("请确认输入  手机号码   和   验证码 ");
      }
    },
    toast3() {
      const toast = Toast.loading({
        duration: 0, // 持续展示 toast
        forbidClick: true,
        message: "验证码发送中"
      });

      let second = 3;
      const timer = setInterval(() => {
        second--;
        if (second) {
          toast.message = `发送中 ${second} 秒`;
        } else {
          clearInterval(timer);
          // 手动清除 Toast
          Toast.clear();
        }
      }, 1000);
    }
  }
};
</script>
<style lang="less">
.van-nav-bar {
  height: 44px;
}
.van-nav-bar__text {
  color: #191a1b;
  font-size: 15px;
}
.van-cell {
  color: #2c3e50;
  width: 325px;
  position: relative;
  left: 50%;
  transform: translateX(-50%);
  outline: none;
}
</style>
<style lang="less" scoped>
.logo {
  text-align: center;
  margin: 50px 0;
  img {
    width: 140px;
    height: 26px;
  }
  background: #fff;
}
.dx-login {
  margin-top: 42px;
  width: 325px;
  position: relative;
  left: 50%;
  transform: translateX(-50%);
  background: #c03131;
  border-radius: 5px;
  color: #fff;
}
.len {
  width: 325px;
}
.btn {
  background: none;
  color: #bdc0c5;
}
#col {
  height: 100vh;
  background: #fff;
  width: 100%;
}
</style>